/**
 * Created by rekey on 12/22/13.
 */
var request = require('request');
var cache = {};
function getMusic(id, callback) {
    var url = 'http://www.xiami.com/song/' + id;
    if (cache[id] && ((Date.now() - cache[id]['timestamp']) < 60000)) {
        callback(cache[id]['data']);
    } else {
        request({
            url : url,
            headers : {
                'User-Agent' : 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25',
                'Referer' : 'http://www.xiami.com/web/spark'
            }
        }, function(err, res, body) {
            var arr = body.match(/var song_id = (.*)/);
            var str = arr ? arr[0] : null;
            var getRet = new Function(str + ';c.data.songs[0].cover = c.data.cover;return c.data.songs[0];');
            cache[id] = {
                data : getRet(),
                timestamp : Date.now()
            };
            callback(cache[id]['data']);
        });
    }
}

exports.getMusic = getMusic;